package cn.tonyqiu.wonuly.service.impl;

import java.util.List;

import cn.tonyqiu.wonuly.dao.AccountDao;
import cn.tonyqiu.wonuly.dao.CategoryDao;
import cn.tonyqiu.wonuly.dao.LookupDao;
import cn.tonyqiu.wonuly.dao.UserDao;
import cn.tonyqiu.wonuly.model.Account;
import cn.tonyqiu.wonuly.model.Category;
import cn.tonyqiu.wonuly.model.User;
import cn.tonyqiu.wonuly.service.LookupManager;
import cn.tonyqiu.wonuly.service.util.UserServiceUtil;

/**
 * Implementation of LookupManager interface to talk to the persistence layer.
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
public class LookupManagerImpl extends UniversalManagerImpl implements
		LookupManager {
	private LookupDao lookupDao;
	private UserDao userDao;
	private CategoryDao categoryDao;
	private AccountDao accountDao;

	/**
	 * @return the accountDao
	 */
	public AccountDao getAccountDao() {
		return accountDao;
	}

	/**
	 * @param accountDao
	 *            the accountDao to set
	 */
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * Method that allows setting the DAO to talk to the data store with.
	 * 
	 * @param dao
	 *            the dao implementation
	 */
	public void setLookupDao(LookupDao lookupDao) {
		this.lookupDao = lookupDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.tonyqiu.wonuly.service.LookupManager#getAccountsByUser()
	 */
	@Override
	public List<Account> getAccountsByUser() {
		String userEmail = UserServiceUtil.getUserEmail();
		User user = userDao.getUserByEmail(userEmail);
		return lookupDao.getAccountsByUser(user.getId());

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.tonyqiu.wonuly.service.LookupManager#getCategoriesByUser()
	 */
	@Override
	public List<Category> getCategoriesByUser() {
		String userEmail = UserServiceUtil.getUserEmail();
		User user = userDao.getUserByEmail(userEmail);
		return lookupDao.getCategoriesByUser(user.getId());

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.tonyqiu.wonuly.service.CategoryManager#getCategoryByCategoryName(cn.tonyqiu.wonuly.model.Category)
	 */
	@Override
	public Category getCategoryByCategoryName(String categoryName)
			throws Exception {
		String userEmail = UserServiceUtil.getUserEmail();
		User user = userDao.getUserByEmail(userEmail);
		Category c = new Category();
		c.setUserId(user.getId());
		c.setName(categoryName);
		return categoryDao.getCategoryByCategoryName(c);
	}

	/**
	 * @return the categoryDao
	 */
	public CategoryDao getCategoryDao() {
		return categoryDao;
	}

	/**
	 * @param categoryDao
	 *            the categoryDao to set
	 */
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	/**
	 * @return the lookupDao
	 */
	public LookupDao getLookupDao() {
		return lookupDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.tonyqiu.wonuly.service.LookupManager#getAccountByAccountName(cn.tonyqiu.wonuly.model.Account)
	 */
	@Override
	public Account getAccountByAccountName(String accountName) throws Exception {
		String userEmail = UserServiceUtil.getUserEmail();
		User user = userDao.getUserByEmail(userEmail);
		Account account = new Account();
		account.setUserId(user.getId());
		account.setName(accountName);
		return accountDao.getAccountByAccountName(account);
	}

}
